//
//  UIViewController+LGStatusBarManager.m
//  HideStatusBarDemo
//
//  Created by lg on 31/10/2019.
//  Copyright © 2019 lg. All rights reserved.
//

#import "UIViewController+LGStatusBarManager.h"
#import <objc/runtime.h>

@implementation UIViewController (LGStatusBarManager)

+ (void)initialize {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
    Method originalMethod = class_getInstanceMethod(self, @selector(_preferredStatusBarVisibility));
#pragma clang diagnostic pop
    Method swizzledMethod = class_getInstanceMethod(self, @selector(lg_swizzled_preferredStatusBarVisibility));
    method_exchangeImplementations(originalMethod, swizzledMethod);
}

- (BOOL)lg_swizzled_preferredStatusBarVisibility {
    if (self.lg_enableStatusBarManager) {
        return self.lg_statusBarHidden;
    } else {
        return [self lg_swizzled_preferredStatusBarVisibility];
    }
}

#pragma mark - lg_enableStatusBarManager
- (BOOL)lg_enableStatusBarManager {
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}

- (void)setLg_enableStatusBarManager:(BOOL)lg_enableStatusBarManager {
    objc_setAssociatedObject(self, @selector(lg_enableStatusBarManager), @(lg_enableStatusBarManager), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self setNeedsStatusBarAppearanceUpdate];
}

#pragma mark - lg_prefersStatusBarHidden
- (BOOL)lg_statusBarHidden {
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}

- (void)setLg_statusBarHidden:(BOOL)lg_prefersStatusBarHidden {
    objc_setAssociatedObject(self, @selector(lg_statusBarHidden), @(lg_prefersStatusBarHidden), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self setNeedsStatusBarAppearanceUpdate];
}

@end

//@implementation UIWindow (LGStatusBarManager)
//
//+(void)initialize {
//    Method originalMethod = class_getClassMethod(self, @selector(_prefersStatusBarHiddenInWindow:targetOrientation:animationProvider:));
//    Method swizzledMethod = class_getClassMethod(self, @selector(lg_swizzled_prefersStatusBarHiddenInWindow:targetOrientation:animationonProvider:));
//    method_exchangeImplementations(originalMethod, swizzledMethod);
//}
//
//+ (BOOL)lg_swizzled_prefersStatusBarHiddenInWindow:(UIWindow *)window targetOrientation:(id)orientation animationonProvider:(id)provider {
//    if (self.lg_enableStatusBarManager) {
//        return self.lg_statusBarHidden;
//    } else {
//        return [self lg_swizzled_prefersStatusBarHiddenInWindow:window targetOrientation:orientation animationonProvider:provider];
//    }
//}
//
//#pragma mark lg_enableStatusBarManager
//+ (BOOL)lg_enableStatusBarManager {
//    return [objc_getAssociatedObject(self, _cmd) boolValue];
//}
//
//+ (void)setLg_enableStatusBarManager:(BOOL)lg_enableStatusBarManager {
//    objc_setAssociatedObject(self, @selector(lg_enableStatusBarManager), @(lg_enableStatusBarManager), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
////    [self setNeedsStatusBarAppearanceUpdate];
//}
//
//#pragma mark lg_statusBarHidden
//+ (BOOL)lg_statusBarHidden {
//    return [objc_getAssociatedObject(self, _cmd) boolValue];
//}
//
//+ (void)setLg_statusBarHidden:(BOOL)lg_statusBarHidden {
//    objc_setAssociatedObject(self, @selector(lg_statusBarHidden), @(lg_statusBarHidden), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
////    [self setNeedsStatusBarAppearanceUpdate];
//}
//
//
//@end
